package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.TradingPoolConfig;
import com.sc.nft.mapper.TradingPoolConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author PC
 * @description 针对表【sc_trading_pool_config(生态池配置)】的数据库操作Dao
 * @createDate 2024-10-26 13:55:44
 */
@Slf4j
@Repository
public class TradingPoolConfigDao extends ServiceImpl<TradingPoolConfigMapper, TradingPoolConfig> {

    /**
     * 获取简单的交易池配置
     *
     * @param id daoId
     * @return
     */
    public TradingPoolConfig getTradingPoolEasyConfig(Long id) {
        LambdaQueryWrapper<TradingPoolConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TradingPoolConfig::getDaoId, id);
        wrapper.eq(TradingPoolConfig::getStatus, Boolean.TRUE);
        wrapper.last("limit 1");
        return this.getOne(wrapper);
    }

    /**
     * 根据藏品获取交易池配置
     *
     * @param collectionId
     * @return
     */
    public List<TradingPoolConfig> listByCollectionId(Long collectionId) {
        LambdaQueryWrapper<TradingPoolConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TradingPoolConfig::getCollectionId, collectionId);
        return list(wrapper);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param daoId
     * @return
     */
    public Page<TradingPoolConfig> page(Page page, Long daoId) {
        LambdaQueryWrapper<TradingPoolConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(daoId), TradingPoolConfig::getDaoId, daoId);
        queryWrapper.orderByDesc(TradingPoolConfig::getId);
        return page(page, queryWrapper);
    }

    /**
     * 按照daoId和状态查询
     *
     * @param daoId
     * @param status
     * @return
     */
    public List<TradingPoolConfig> listByDaoAndStatus(Long daoId, Boolean status) {
        LambdaQueryWrapper<TradingPoolConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TradingPoolConfig::getDaoId, daoId);
        wrapper.eq(TradingPoolConfig::getStatus, status);
        return list(wrapper);
    }

    /**
     * 增加库存
     *
     * @param id
     * @param stock
     * @return
     */
    public Boolean addStock(Long id, Integer stock) {
        return baseMapper.addStock(id, stock) > 0;
    }

    /**
     * 减少库存
     *
     * @param id
     * @param stock
     * @return
     */
    public Boolean reduceStock(Long id, Integer stock) {
        return baseMapper.reduceStock(id, stock) > 0;
    }

    /**
     * 配置分润模版id
     * @param shareProfitPanelId 模版id
     * @param id 配置id
     */
    public Boolean updateShareProfitPanel(Long shareProfitPanelId, Long id) {
        LambdaUpdateWrapper<TradingPoolConfig> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(TradingPoolConfig::getShareProfitPanelId, shareProfitPanelId);
        wrapper.eq(TradingPoolConfig::getId, id);
        return update(wrapper);
    }
    /**
     * 增加金额
     *
     * @param id
     * @param amount
     * @return
     */
    public Boolean addAmount(Long id, BigDecimal amount) {
        return baseMapper.addAmount(id, amount) > 0;
    }

    /**
     * 减少金额
     *
     * @param id
     * @param amount
     * @return
     */
    public Boolean reduceAmount(Long id, BigDecimal amount) {
        return baseMapper.reduceAmount(id, amount) > 0;
    }

    /**
     * 扣资金增加库存
     * @param id
     * @param price
     * @param sumCount
     * @return
     */
    public Boolean sellInventoryDeduction(Long id, BigDecimal price, Integer sumCount) {
        return baseMapper.sellInventoryDeduction(id, price, sumCount) > 0;
    }

    /**
     * 加资金减库存
     * @param id
     * @param price
     * @param sumCount
     * @return
     */
    public Boolean buyInventoryDeduction(Long id, BigDecimal price, Integer sumCount) {
        return baseMapper.buyInventoryDeduction(id, price, sumCount) > 0;
    }

    public List<TradingPoolConfig> getActivatedTradingPoolConfigList() {
        LambdaQueryWrapper<TradingPoolConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TradingPoolConfig::getStatus, Boolean.TRUE);
        return list(wrapper);
    }

    /**
     * 扣交易池库存
     * @param id
     * @param destroyNft
     * @return
     */
    public Boolean subInventory(Long id, Integer destroyNft) {
        return baseMapper.subInventory(id, destroyNft) > 0;
    }

    public Boolean subAmount(Long id, BigDecimal destroyAmount) {
        return baseMapper.subAmount(id, destroyAmount) > 0;
    }

    /**
     * 同步今日交易数据
     * @param id
     * @param todayHighest
     * @param todayLowest
     * @param toDayVolume
     */
    public void updateDealToday(Long id, BigDecimal todayHighest, BigDecimal todayLowest, Integer toDayVolume) {
        baseMapper.updateDealToday(id,todayHighest,todayLowest,toDayVolume);
    }

    public void initTradingPool() {
        LambdaUpdateWrapper<TradingPoolConfig> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(TradingPoolConfig::getTodayHighest,BigDecimal.ZERO);
        wrapper.set(TradingPoolConfig::getTodayLowest,BigDecimal.ZERO);
        wrapper.set(TradingPoolConfig::getTodayVolume,0);
        wrapper.eq(TradingPoolConfig::getStatus,Boolean.TRUE);
        update(wrapper);
    }
}
